const lodash = require('lodash/lodash')
const findDeep = require('deepdash/getFindDeep')(lodash)
const treeData = require('./treeData')

const node = {
  name: 'level2',
  value: 2
}

const propMap = {
  keyProp: 'value',
  childrenProp: 'children',
  includeSelf: true
}

function findSiblings (treeData, node, propMap) {
  const deepOptions = {
    childrenPath: 'children'
  }
  const searchValue = node[propMap.keyProp]
  const targetMeta = findDeep(treeData, function (item, key, parent) {
    const itemValue = item[propMap.keyProp]
    if (itemValue === searchValue) return true
  }, deepOptions)

  if (!targetMeta) return []
  const { parent } = targetMeta

  const siblings =  parent[propMap.childrenProp] || []
  return propMap.includeSelf ? siblings : siblings.filter(item => item[propMap.keyProp] !== node[propMap.keyProp])
}


const siblingsNode = findSiblings(treeData, node, propMap)

console.log(`${node.name} of node siblings: \n`, siblingsNode)

console.log(`${node.name} of node siblings: \n`, findSiblings([treeData], node, propMap))
